<?php

function Char_cv($msg) {
	if (is_array ( $msg )) {
		foreach ( $msg as $a => $b ) {
			$msg [$a] = Char_cv ( $b );
		}
	}
	$msg = str_replace ( '%20', '', $msg );
	$msg = str_replace ( '%27', '', $msg );
	$msg = str_replace ( '*', '', $msg );
	$msg = str_replace ( "\"", '', $msg );
	$msg = str_replace ( '&amp;', '&', $msg );
	$msg = str_replace ( '&nbsp;', ' ', $msg );
	$msg = str_replace ( ';', '', $msg );
	$msg = str_replace ( '"', '&quot;', $msg );
	$msg = str_replace ( "'", '&#039;', $msg );
	$msg = str_replace ( "<", "&lt;", $msg );
	$msg = str_replace ( ">", "&gt;", $msg );
	$msg = str_replace ( '(', '', $msg );
	$msg = str_replace ( ')', '', $msg );
	$msg = str_replace ( "{", '', $msg );
	$msg = str_replace ( '}', '', $msg );
	$msg = str_replace ( "\t", "   &nbsp;  &nbsp;", $msg );
	$msg = str_replace ( "\r", "", $msg );
	$msg = str_replace ( "   ", " &nbsp; ", $msg );
	$msg = str_replace ( 'select', '', $msg );
	$msg = str_replace ( 'insert', '', $msg );
	$msg = str_replace ( 'update', '', $msg );
	$msg = str_replace ( 'delete', '', $msg );
	$msg = str_replace ( 'union', '', $msg );
	$msg = str_replace ( 'into', '', $msg );
	$msg = str_replace ( 'load_file', '', $msg );
	$msg = str_replace ( 'outfile', '', $msg );
	return $msg;
}
function fun_url_ck() {
	if (preg_replace ( "/https?:\/\/([^\:\/]+).*/i", "\\1", $_SERVER ['HTTP_REFERER'] ) !== preg_replace ( "/([^\:]+).*/", "\\1", $_SERVER ['HTTP_HOST'] )) {
		header ( "Location: http://{$_SERVER['HTTP_HOST']}" );
		exit ();
	}
}
function HtmlReplace($str, $rptype = -1) {
	$str = stripslashes ( $str );
	if ($rptype == 0) {
		$str = htmlspecialchars ( $str );
	} else if ($rptype == 1) {
		$str = htmlspecialchars ( $str );
		$str = str_replace ( "　", ' ', $str );
		$str = ereg_replace ( "[\r\n\t ]{1,}", ' ', $str );
	} else if ($rptype == 2) {
		$str = htmlspecialchars ( $str );
		$str = str_replace ( "　", '', $str );
		$str = ereg_replace ( "[\r\n\t ]", '', $str );
	} else {
		$str = ereg_replace ( "[\r\n\t ]{1,}", ' ', $str );
		$str = eregi_replace ( 'script', 'ｓｃｒｉｐｔ', $str );
		$str = eregi_replace ( "<[/]{0,1}(link|meta|ifr|fra)[^>]*>", '', $str );
	}
	return addslashes ( $str );
}
function Init_GP($keys, $method = 'GP', $htmcv = 1) {
	! is_array ( $keys ) && $keys = array ($keys );
	$array = array ();
	foreach ( $keys as $val ) {
		$array [$val] = NULL;
		if ($method != 'P' && isset ( $_GET [$val] )) {
			$array [$val] = $_GET [$val];
		} elseif ($method != 'G' && isset ( $_POST [$val] )) {
			$array [$val] = $_POST [$val];
		}
		$htmcv && $array [$val] = Char_cv ( $array [$val] );
	}
	return $array;
}
function InitGP($keys, $method = 'GP', $htmcv = 0) {
	! is_array ( $keys ) && $keys = array ($keys );
	foreach ( $keys as $val ) {
		$GLOBALS [$val] = NULL;
		if ($method != 'P' && isset ( $_GET [$val] )) {
			$GLOBALS [$val] = $_GET [$val];
		} elseif ($method != 'G' && isset ( $_POST [$val] )) {
			$GLOBALS [$val] = $_POST [$val];
		}
		$htmcv && $GLOBALS [$val] = Char_cv ( $GLOBALS [$val] );
	}
}
function GetNum($fnum) {
	$nums = array ("０", "１", "２", "３", "４", "５", "６", "７", "８", "９" );
	$fnums = array ("0", "1", "2", "3", "4", "5", "6", "7", "8", "9" );
	$fnum = str_replace ( $nums, $fnums, $fnum );
	$fnum = ereg_replace ( "[^0-9\.-]", '', $fnum );
	if ($fnum == '') {
		$fnum = 0;
	}
	return $fnum;
}
function getdotstring($string, $vartype, $allownull = false, $varscope = array(), $sqlmode = 1, $unique = true) {
	if (is_array ( $string )) {
		$stringarr = $string;
	} else {
		if (substr ( $string, 0, 1 ) == '$') {
			return $string;
		}
		$string = str_replace ( '，', ',', $string );
		$string = str_replace ( ' ', ',', $string );
		$stringarr = explode ( ',', $string );
	}
	$newarr = array ();
	foreach ( $stringarr as $value ) {
		$value = trim ( $value );
		if ($vartype == 'int') {
			$value = intval ( $value );
		}
		if (! empty ( $varscope )) {
			if (in_array ( $value, $varscope )) {
				$newarr [] = $value;
			}
		} else {
			if ($allownull) {
				$newarr [] = $value;
			} else {
				if (! empty ( $value ))
					$newarr [] = $value;
			}
		}
	}
	if ($unique)
		$newarr = sarray_unique ( $newarr );
	if ($vartype == 'int') {
		$string = implode ( ',', $newarr );
	} else {
		if ($sqlmode) {
			$string = '\'' . implode ( '\',\'', $newarr ) . '\'';
		} else {
			$string = implode ( ',', $newarr );
		}
	}
	return $string;
}
function sarray_unique($array) {
	$newarray = array ();
	if (! empty ( $array ) && is_array ( $array )) {
		$array = array_unique ( $array );
		foreach ( $array as $value ) {
			$newarray [] = $value;
		}
	}
	return $newarray;
}
function down_xls($data, $keynames, $name = 'down') {
	$xlsdata [] = array_values ( $keynames );
	$time = array ('addtime', 'uploadtime', 'lastlogin', 'edittime', 'starttime', 'endtime', 'ordertime' );
	foreach ( $data as $o ) {
		$line = array ();
		foreach ( $keynames as $k => $v ) {
			if (in_array ( $k, $time ))
				$o [$k] = date ( "Y-m-d H:i:s", $o [$k] );
			$line [] = $o [$k];
		}
		$xlsdata [] = $line;
	}
	import ( "@.ORG.Excel" );
	$xls = new Excel ();
	$xls->addArray ( $xlsdata );
	$xls->generateXML ( $name );
}
function price($data) {
	$tmp = intval ( $data );
	if ($data == $tmp)
		$data = $tmp;
	return $data;
}
function sub_head($url) {
	$arr = explode ( '/', $url );
	$head = end ( $arr );
	return $head;
}
function getlocaltion($ip) {
	import ( "@.ORG.IpLocation" );
	$iplocation = new IpLocation ( QQWRY );
	$arr = $iplocation->getlocation ( $ip );
	$str = iconv ( 'GB2312', 'UTF-8//IGNORE', $arr ['country'] );
	return $str;
}
function sendsms($tel, $msg) {
	$config = C ( 'SYSCONFIG' );
	import ( "@.ORG.Http" );
	$msg = str_replace ( '[webname]', $config ['webname'], $msg );
	$msg = urlencode ( $msg );
	$url = $config ['sms_Interface'];
	$url = str_replace ( '[tel]', $tel, $url );
	$url = str_replace ( '[msg]', $msg, $url );
	$data = Http::fsockopen_download ( $url );
	if ($data == $config ['sms_success']) {
		return true;
	} else {
		return false;
	}
}
function isurl($url) {
	return preg_match ( '/^http:\/\/[A-Za-z0-9]+\.[A-Za-z0-9]+[\/=\?%\-&_~`@[\]\':+!]*([^<>\"\"])*$/', $url ) === 1;
}
function formatip($ip) {
	$arr = explode ( '.', $ip );
	$arr [3] = '*';
	$ip = implode ( '.', $arr );
	return $ip;
}
function checkKey() {
	import ( "@.ORG.Xxtea" );
	if (! file_exists ( ROOT_PATH . "/Public/key.dat" )) {
		show ();
	}
	$base64_key = 'yuns';
	$xxtea_key = 'blon';
	$des_key = 'hzj';
	$str = @file_get_contents ( ROOT_PATH . "/Public/key.dat" );
	$str = Xxtea::decrypt ( $str, $xxtea_key );
	$arr = explode ( ',', $str );
	$host = $_SERVER ['HTTP_HOST'];
	if (in_array ( $host, $arr )) {
		return true;
	}
	show ();
}
function show() {
}

function lucker($dot) {
	$dot = intval ( $dot );
	$dot = max ( $dot, 0 );
	$dot = min ( $dot, 100 );
	$one = rand ( 1, 100 );
	$total = range ( 1, 100 );
	shuffle ( $total );
	$range = array ();
	for($i = 0; $i < $dot; $i ++) {
		$range [] = $total [$i];
	}
	return in_array ( $one, $range );
}

?>